package ru.blight.parser;

import org.apache.commons.io.FileUtils;
import ru.blight.model.Book;
import ru.blight.model.Module;
import sun.misc.Regexp;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created with IntelliJ IDEA.
 * User: ilya
 * Date: 8/12/13
 * Time: 11:50 PM
 * To change this template use File | Settings | File Templates.
 */
public class Parser {
    private String bookRegexString  = "";

    public Module parse(File moduleDir) throws IOException {
        File infoFile = FileUtils.getFile(moduleDir, "bibleqt.ini");
        String infoString = FileUtils.readFileToString(infoFile);

        Module module = new Module();
        parseModuleInfo(infoString, module);
        module.setBooks(parseBooks(infoString));

        return module;
    }

    private List<Book> parseBooks(String infoString) {


        return null;  //To change body of created methods use File | Settings | File Templates.
    }

    private void parseModuleInfo(String infoString, Module module) {
        Pattern pattern = Pattern.compile("BibleName = (.*)");
        Matcher matcher = pattern.matcher(infoString);
        if (matcher.groupCount() == 2) {
            module.setName(matcher.group(1));
        }

        pattern = Pattern.compile("BibleShortName = (.*)");
        matcher = pattern.matcher(infoString);
        if (matcher.groupCount() == 2) {
            module.setSortName(matcher.group(1));
        }
    }
}
